package util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import com.alibaba.druid.pool.DruidDataSource;

public class DBUtils {
	
	private static DataSource dataSource;
	
	static {
		
		try {
			Properties p = new Properties();
			p.load(new FileInputStream("./conf/jdbc.properties"));
			String driver = p.getProperty("jdbc.driver");
			String url = p.getProperty("jdbc.url");
			String username = p.getProperty("jdbc.username");
			String password = p.getProperty("jdbc.password");
			
			DruidDataSource dds = new DruidDataSource();
			dds.setDriverClassName(driver);
			dds.setUrl(url);
			dds.setUsername(username);
			dds.setPassword(password);
			
			dds.setMinIdle(10);
			dds.setInitialSize(10);
			dds.setMaxActive(30);
			dds.setMaxWait(3600);
			
			dataSource = dds;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static Connection getConnection() throws SQLException {
		return dataSource.getConnection();
	}
}
